package cn.labbook.bluetooth.controller.v2;

import cn.labbook.bluetooth.common.ResultDto;
import cn.labbook.bluetooth.controller.AbstractController;
import cn.labbook.bluetooth.entity.Ammeter;
import cn.labbook.bluetooth.entity.ApplyRecord;
import cn.labbook.bluetooth.service.IAmmeterService;
import cn.labbook.bluetooth.service.IApplyPayService;
import cn.labbook.bluetooth.service.IApplyRecordService;
import cn.labbook.bluetooth.util.weixin.pay.XmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: zhangyang
 * @Date: 2023/4/13 14:54
 * @Desc: v2版本 微信支付 controller
 */
@Controller
@RequestMapping("/v2/weixin")
public class V2WeiXinPaymentController extends AbstractController {

    @Autowired
    private IApplyPayService applyPayService;
    @Autowired
    private IAmmeterService ammeterService;
    @Autowired
    private IApplyRecordService applyRecordService;


    @RequestMapping("/payment")
    @ResponseBody
    public ResultDto payment(
            @RequestParam String ammeterId, @RequestParam String openId,
            @RequestParam String totalFee, @RequestParam String body
    ) {
        Ammeter ammeter = ammeterService.getById(ammeterId);
        if (Objects.isNull(ammeter)) {
            return fail("获取电表失败");
        }

        if (applyRecordService.exist(openId, ApplyRecord.Status.IN.getKey())) {
            return fail("有电费申请在处理中");
        }
        return success(applyPayService.payment(ammeterId, openId,  totalFee, body));
    }

    @RequestMapping("/callbackTenant")
    @ResponseBody
    public String callbackTenant(HttpServletRequest request) {
        try {
            Map<String, Object> dataMap = XmlUtil.parseXML(request);
            applyPayService.callbackTenant(dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "<xml>\n" +
                "  <return_code><![CDATA[SUCCESS]]></return_code>\n" +
                "  <return_msg><![CDATA[OK]]></return_msg>\n" +
                "</xml>";
    }

    @RequestMapping("/paymentLandlord")
    @ResponseBody
    public ResultDto paymentLandlord(
            @RequestParam Integer applyRecordId
    ) {
        return success(applyPayService.paymentLandlord(applyRecordId));
    }

    @RequestMapping("/callbackLandlord")
    @ResponseBody
    public String callbackLandlord(HttpServletRequest request) {
        try {
            Map<String, Object> dataMap = XmlUtil.parseXML(request);
            applyPayService.callbackLandlord(dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "<xml>\n" +
                "  <return_code><![CDATA[SUCCESS]]></return_code>\n" +
                "  <return_msg><![CDATA[OK]]></return_msg>\n" +
                "</xml>";
    }

}
